<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php
/**
 * @var \Magento\Customer\Block\Adminhtml\System\Config\Validatevat $block
 */
?>
<script>
require(['prototype'], function(){

//<![CDATA[
    function validateVat() {
        var validationMessage = $('validation_result');

        params = {
            country: $('<?php /* @escapeNotVerified */ echo $block->getMerchantCountryField(); ?>').value,
            vat: $('<?php /* @escapeNotVerified */ echo $block->getMerchantVatNumberField(); ?>').value
        };

        new Ajax.Request('<?php echo $this->helper(\Magento\Framework\EscapeHelper::class)->escapeJs($block->escapeUrl($block->getAjaxUrl())) ?>', {
            parameters: params,
            onSuccess: function(response) {
                var result = '<?php echo $this->helper(\Magento\Framework\EscapeHelper::class)->escapeJs($block->escapeHtml(__('Error during VAT Number verification.'))) ?>';
                try {
                    if (response.responseText.isJSON()) {
                        response = response.responseText.evalJSON();
                        result = response.message;
                    }
                    if (response.valid == 1) {
                        validationMessage.removeClassName('hidden').addClassName('success')
                    } else {
                        validationMessage.removeClassName('hidden').addClassName('error')
                    }
                } catch (e) {
                    validationMessage.removeClassName('hidden').addClassName('error')
                }
                validationMessage.update(result);
            }
        });
    }

    window.validateVat = validateVat;
//]]>

});
</script>
<div class="actions actions-validate-vat">
    <div id="validation_result" class="message-validation hidden"></div>
    <button onclick="javascript:validateVat(); return false;" class="action-validate-vat" type="button" id="<?php echo $block->getHtmlId() ?>">
        <span><?php echo $block->escapeHtml($block->getButtonLabel()) ?></span>
    </button>
</div>
